package chapter18.tankwar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @Auther: Tx
 * @Date: 2022/7/14 11:26
 */
//为了监听键盘事件，实现keyListener
class MyPanel extends JPanel implements KeyListener, Runnable {
    myTank myTank = null;//初始化自己的坦克

    Vector<EnemyTank> enemyTanks = new Vector<>();
    //敌人坦克
    int enemyTankNum = 3;
   //初始化敌方坦克数量
    public MyPanel() {
        myTank = new myTank(500,500);
        for (int i = 0; i < enemyTankNum; i++) {
           EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 100);
            enemyTank.setDirection(2);
            enemyTanks.add(enemyTank);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        drawTank(myTank.getX(), myTank.getY(), g, myTank.getDirection(), 0);

//        画出坦克的子弹
        if (myTank.bullet != null && myTank.bullet.isLive) {
            System.out.println("子弹被绘制");
            g.draw3DRect(myTank.bullet.x, myTank.bullet.y, 5, 5, false);
        }

//            绘制敌方坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
        }
    }


    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向，来绘制坦克
        switch (direct) {
            case 0://绘制向上的坦克
                g.fill3DRect(x, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 10, y + 15, 20, 30, false);
                g.fillOval(x + 10, y + 20, 20, 20);//坦克的盖子
                g.drawLine(x + 20, y + 30, x + 20, y);//坦克的炮筒
                repaint();
                break;
            case 1://绘制向右的坦克
                g.fill3DRect(x, y, 60, 10, false);//画上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画下边的轮子
                g.fill3DRect(x + 15, y + 10, 30, 20, false);//坦克的中间部分
                g.fillOval(x + 20, y + 10, 20, 20);//坦克的盖子
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//坦克的炮筒

                break;
            case 2://绘制向下的坦克
                g.fill3DRect(x, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 10, y + 15, 20, 30, false);
                g.fillOval(x + 10, y + 20, 20, 20);//坦克的盖子
                g.drawLine(x + 20, y + 30, x + 20, y + 60);//坦克的炮筒
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);//画上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画下边的轮子
                g.fill3DRect(x + 15, y + 10, 30, 20, false);//坦克的中间部分
                g.fillOval(x + 20, y + 10, 20, 20);//坦克的盖子
                g.drawLine(x + 30, y + 20, x, y + 20);//坦克的炮筒
                break;
        }


    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            myTank.setDirection(0);
            //修改坦克的坐标
            myTank.moveUp();

        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            myTank.setDirection(1);
            myTank.moveRight();

        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            myTank.setDirection(2);
            myTank.moveDown();

        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            myTank.setDirection(3);
            myTank.moveLeft();

        }

        if (e.getKeyCode() == KeyEvent.VK_J) {
            System.out.println("开始射击");
            myTank.shot();
        }
//        面板重绘
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("重绘");
            this.repaint();
        }
    }
}
